package cn.com.chec.en.controller.admin;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.chec.en.dao.MsgInfoDao;
import cn.com.chec.en.dao.WebsiteInfoDao;
import cn.com.chec.en.model.domain.MsgInfo;
import cn.com.chec.en.model.domain.WebsiteInfo;
import cn.com.chec.en.model.dto.AnalyzerInfo;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.TimeUtils;
import cn.com.chec.en.util.WebUtils;

@Controller
public class AnalyzerController {
	protected static final Logger logger = Logger
			.getLogger(AnalyzerController.class);

	@Resource
	private WebsiteInfoDao websiteInfoDao;

	@Resource
	private MsgInfoDao msgInfoDao;

	@RequestMapping({ "/admin/analyzer/list.do" })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/contentInfo/analyzerList");
		int websiteId = WebUtils.getRequestParameterAsInt(request, "websiteId",
				0);
		String startTime = WebUtils.getRequestParameterAsString(request,
				"startTime");
		String endTime = WebUtils.getRequestParameterAsString(request,
				"endTime");

		if ((StringUtils.isBlank(startTime))
				|| (!startTime
						.matches("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})"))) {
			startTime = TimeUtils.getDate(new Date(), -7) + " 00:00:00";
		}
		if ((StringUtils.isBlank(endTime))
				|| (!endTime
						.matches("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})"))) {
			endTime = TimeUtils.getDate(new Date(), 0) + " 23:59:59";
		}

		MsgInfo query = new MsgInfo();
		query.setWebsiteId(websiteId);
		query.setStartTime(startTime);
		query.setEndTime(endTime);

		List<AnalyzerInfo> analyzerInfoList = this.msgInfoDao.getAnalyzerList(query);
		List<WebsiteInfo> websiteInfoList = this.websiteInfoDao.getSubList(null, 0,
				2147483647);
		map.put("analyzerInfoList", analyzerInfoList);
		map.put("query", query);
		map.put("websiteInfoList", websiteInfoList);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/analyzer/detail.do" })
	public ModelAndView detail(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/contentInfo/analyzerDetail");
		String title = "消息详情";
		int websiteId = WebUtils.getRequestParameterAsInt(request, "websiteId",
				0);
		int type = WebUtils.getRequestParameterAsInt(request, "type", 0);
		String startTime = WebUtils.getRequestParameterAsString(request,
				"startTime");
		String endTime = WebUtils.getRequestParameterAsString(request,
				"endTime");

		if ((StringUtils.isBlank(startTime))
				|| (!startTime
						.matches("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})"))) {
			startTime = TimeUtils.getDate(new Date(), -7) + " 00:00:00";
		}
		if ((StringUtils.isBlank(endTime))
				|| (!endTime
						.matches("(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})"))) {
			endTime = TimeUtils.getDate(new Date(), 0) + " 23:59:59";
		}
		MsgInfo query = new MsgInfo();
		query.setWebsiteId(websiteId);
		query.setStartTime(startTime);
		query.setEndTime(endTime);

		WebsiteInfo websiteInfo = this.websiteInfoDao.getWebsite(websiteId);

		int curPage = StringUtils.isNotBlank(request.getParameter("curPage")) ? Integer
				.parseInt(request.getParameter("curPage")) : 1;
		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);

		List<MsgInfo> msgInfoList = null;

		switch (type) {
		case 1:
			msgInfoList = this.msgInfoDao.getMsgAnalyzerDetailList(1, query,
					pageBean.getStart(), pageBean.getRowsPerPage());
			pageBean.setMaxRowCount(this.msgInfoDao.getMsgAnalyzerDetailCount(
					1, query));
			title = websiteInfo.getName() + "发送详情";
			break;
		case 2:
			msgInfoList = this.msgInfoDao.getMsgAnalyzerDetailList(2, query,
					pageBean.getStart(), pageBean.getRowsPerPage());
			pageBean.setMaxRowCount(this.msgInfoDao.getMsgAnalyzerDetailCount(
					2, query));
			title = websiteInfo.getName() + "接收详情";
			break;
		case 3:
			msgInfoList = this.msgInfoDao.getMsgAnalyzerDetailList(3, query,
					pageBean.getStart(), pageBean.getRowsPerPage());
			pageBean.setMaxRowCount(this.msgInfoDao.getMsgAnalyzerDetailCount(
					3, query));
			title = websiteInfo.getName() + "已读详情";
			break;
		case 4:
			msgInfoList = this.msgInfoDao.getMsgAnalyzerDetailList(4, query,
					pageBean.getStart(), pageBean.getRowsPerPage());
			pageBean.setMaxRowCount(this.msgInfoDao.getMsgAnalyzerDetailCount(
					4, query));
			title = websiteInfo.getName() + "采用详情";
			break;
		default:
			msgInfoList = Collections.emptyList();
		}

		pageBean.setMaxPage();
		pageBean.setPageNoList();
		map.put("msgInfoList", msgInfoList);
		map.put("pageBean", pageBean);
		map.put("title", title);
		map.put("query", query);
		map.put("type", Integer.valueOf(type));
		mav.addAllObjects(map);
		return mav;
	}
}